﻿using FlyIL;
using FlyRemote.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FuckVmp.Asm
{

    public class MyReader : IReader
    {

        public ReadGroup readGroup { get; set; }

        public ulong Base => throw new NotImplementedException();

        public int RegionSize => throw new NotImplementedException();

        public byte[] Buffer => throw new NotImplementedException();

        public bool IsOutOfModule(ulong addr)
        {
            throw new NotImplementedException();
        }

        public bool IsZeroMemory(ulong addr, int size)
        {
            throw new NotImplementedException();
        }

        public ushort? Read16(ulong addr)
        {
            if (readGroup.BaseAddress <= addr && addr <= ReadGroup.EndAddress - 2)
            {
                ulong offset = addr - readGroup.BaseAddress;
                return BitConverter.ToUInt16(ReadGroup.CurrentValues, (int)offset);

            }

            return null;
        }

        public uint? Read32(ulong addr)
        {
            if (ReadGroup.BaseAddress <= addr && addr <= ReadGroup.EndAddress - 4)
            {
                ulong offset = addr - ReadGroup.BaseAddress;
                return BitConverter.ToUInt32(ReadGroup.CurrentValues, (int)offset);

            }

            return null;
        }

        public ulong? Read64(ulong addr)
        {
            if (ReadGroup.BaseAddress <= addr && addr <= ReadGroup.EndAddress - 8)
            {
                ulong offset = addr - ReadGroup.BaseAddress;
                return BitConverter.ToUInt64(ReadGroup.CurrentValues, (int)offset);

            }

            return null;
        }

        public byte? Read8(ulong addr)
        {
            if (ReadGroup.BaseAddress <= addr && addr <= ReadGroup.EndAddress)
            {
                return ReadGroup.CurrentValues[addr - ReadGroup.BaseAddress];
            }

            return null;

        }
    }








    public class MyLego : ILego
    {


        ReadGroup Group;
        byte Arch;
        SnapshotCell Cell;


        public MyLego(byte Arch, SnapshotCell cell)
        {
            this.Cell = cell;
            this.Arch = Arch;
            Group = cell.ReadGroup;
            myReader = new MyReader { ReadGroup = cell.ReadGroup };

            codeReader = new MyCodeReader { Group = cell.ReadGroup, Offset = cell.ReadGroupOffset };
        }

        public override bool IsOutOfModule(ulong addr)
        {
            /// 跳出到模块外
            if (addr < Group.BaseAddress || Group.EndAddress < addr)
                return true;
            return false;
        }


        public override void ReOffset(ulong addr)
        {
            ((MyCodeReader)codeReader).Offset = (int)(addr - Group.BaseAddress);
        }
    }
}
